package cn.tedu.ivos.dict.service.impl;

import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictParam;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> selectAll(DictQuery dictQuery) {
        log.debug("开始处理【查询字典列表】的业务，参数：{}", dictQuery);
        List<DictVO> list = dictMapper.selectAll(dictQuery);
        return list;
    }

    @Override
    public void save(DictParam dictParam) {
        log.debug("开始处理【保存字典】的业务，参数：{}", dictParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictParam, dict);
        if (dict.getId() == null){
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }else {
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除字典】的业务，参数：{}", id);
        dictMapper.delete(id);
        // 删除字典,则字典下管理的字典项也要随之删除
        dictOptionMapper.deleteDict(id);
    }
}
